package com.city.dispatch.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.city.dispatch.R;
import com.city.dispatch.core.common.HttpHelper;
import com.city.dispatch.core.common.SharedPreferencesUrls;
import com.city.dispatch.core.common.UIHelper;
import com.city.dispatch.core.common.Urls;
import com.city.dispatch.core.widget.CustomDialog;
import com.city.dispatch.core.widget.LoadingDialog;
import com.city.dispatch.model.ResultConsel;
import com.city.dispatch.swipebacklayout.app.SwipeBackActivity;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import org.apache.http.Header;

import java.net.URLEncoder;


/**
 * Created by yuanyi on 2019/12/9.
 */
public class CarUnbindDetailActivity extends SwipeBackActivity implements View.OnClickListener{

    private Context context;

    private LinearLayout ll_back;
    private TextView title;

    private LoadingDialog loadingDialog;
    private LinearLayout backImg;
    private Button submitBtn;

    private ImageView iv_bike;
    private TextView tv_car_type;
    private TextView tv_car_number;
    private TextView tv_order_amount;
    private TextView tv_order_sn;
    private TextView tv_payment_name;
    private TextView tv_car_start_time;
    private TextView tv_car_end_time;
    private TextView tv_payment_time;
    private TextView tv_price;
    private TextView tv_continued_price;
    private TextView tv_credit_score_desc;
    private TextView tv_credit_score_desc2;
    private TextView tv_each_free_time;
    private TextView tv_cycling_time;

    private RelativeLayout rl_payment_name;
    private RelativeLayout rl_each_free_time;
    private RelativeLayout rl_payment_time;

    private int order_id;
    private boolean isOrder;

    private TextView tv_carmodel_name;
    private TextView tv_codenum;
    private TextView tv_lock_name;
    private TextView tv_deviceuuid;
    private TextView tv_bleid;

    private String carmodel_name;
    private String codenum;
    private String lock_name;
    private String deviceuuid;
    private String bleid;

    CustomDialog.Builder customBuilder;
    private CustomDialog customDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_unbind_detail);
        context = this;

        initView();
    }

    private void initView(){
        ll_back = (LinearLayout) findViewById(R.id.ll_backBtn);
        title = (TextView) findViewById(R.id.mainUI_title_titleText);
        title.setText("车辆解绑");

        loadingDialog = new LoadingDialog(context);
        loadingDialog.setCancelable(false);
        loadingDialog.setCanceledOnTouchOutside(false);

        backImg = (LinearLayout) findViewById(R.id.ll_backBtn);
        submitBtn = (Button) findViewById(R.id.bt_unbind);
        tv_carmodel_name = (TextView)findViewById(R.id.tv_carmodel_name);
        tv_codenum = (TextView)findViewById(R.id.tv_codenum);
        tv_lock_name = (TextView)findViewById(R.id.tv_lock_name);
        tv_deviceuuid = (TextView)findViewById(R.id.tv_deviceuuid);
        tv_bleid = (TextView)findViewById(R.id.tv_bleid);

        backImg.setOnClickListener(this);
        submitBtn.setOnClickListener(this);

        carmodel_name = getIntent().getStringExtra("carmodel_name");
        codenum = getIntent().getStringExtra("codenum");
        lock_name = getIntent().getStringExtra("lock_name");
        deviceuuid = getIntent().getStringExtra("deviceuuid");
        bleid = getIntent().getStringExtra("bleid");

        tv_carmodel_name.setText(carmodel_name);
        tv_codenum.setText(codenum);
        tv_lock_name.setText(lock_name);
        tv_deviceuuid.setText(deviceuuid);
        tv_bleid.setText(bleid);

//        order_detail();
    }

    private void order_detail() {
        Log.e("oda===order_detail", order_id+"===");

//        HttpHelper.get(context, Urls.order_detail+order_id, new TextHttpResponseHandler() {
//            @Override
//            public void onStart() {
//                onStartCommon("正在加载");
//            }
//            @Override
//            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
//                onFailureCommon(throwable.toString());
//            }
//
//            @Override
//            public void onSuccess(int statusCode, Header[] headers, final String responseString) {
//
//                m_myHandler.post(new Runnable() {
//                    @Override
//                    public void run() {
//
//                        try {
//                            ResultConsel result = JSON.parseObject(responseString, ResultConsel.class);
//
//                            Log.e("oda===order_detail1", responseString + "===" + result.data);
//
//                            BillBean bean = JSON.parseObject(result.getData(), BillBean.class);
//
//                            if (loadingDialog != null && loadingDialog.isShowing()) {
//                                loadingDialog.dismiss();
//                            }
//
//                            if(null != bean.getOrder_sn()){
//                                Log.e("oda===order_detail2", bean.getOrder_sn()+"===" + bean.getCar_number()+"===" + bean.getCar_type());
//
//                                String car_type = "";
//
//
//                                if(bean.getIs_over_area()==0){
//                                    if(bean.getCar_type()==1){
//                                        car_type = "单车";
//                                        tv_car_type.setTextColor(0xFF3A3A3A);
//                                    }else{
//                                        car_type = "助力车";
//                                        tv_car_type.setTextColor(0xFF3A3A3A);
//                                    }
//                                }else{
//                                    if(bean.getCar_type()==1){
//                                        car_type = "单车(超区)";
//                                        tv_car_type.setTextColor(0xFFFF0000);
//                                    }else{
//                                        car_type = "助力车(超区)";
//                                        tv_car_type.setTextColor(0xFFFF0000);
//                                    }
//                                }
//
//                                tv_car_type.setText(car_type);
//                                tv_car_number.setText(""+bean.getCar_number());
//                                tv_order_amount.setText(""+bean.getOrder_amount());
//                                tv_order_sn.setText(""+bean.getOrder_sn());
//                                tv_payment_name.setText(""+bean.getPayment_name());
//                                tv_car_start_time.setText(""+bean.getCar_start_time());
//                                tv_car_end_time.setText(""+bean.getCar_end_time());
//                                tv_payment_time.setText(""+bean.getPayment_time());
//                                tv_price.setText(""+bean.getPrice());
//                                tv_continued_price.setText(""+bean.getContinued_price());
//                                tv_credit_score_desc.setText("("+bean.getCredit_score_desc()+")");
//                                tv_credit_score_desc2.setText("("+bean.getCredit_score_desc()+")");
//
//                                if(!"0".equals(bean.getEach_free_time())){
//                                    rl_each_free_time.setVisibility(View.VISIBLE);
//                                    tv_each_free_time.setText(bean.getEach_free_time()+"分钟");
//                                }else{
//                                    rl_each_free_time.setVisibility(View.GONE);
//                                }
//
//                                if("".equals(bean.getCredit_score_desc())){
//                                    tv_credit_score_desc.setVisibility(View.GONE);
//                                    tv_credit_score_desc2.setVisibility(View.GONE);
//                                    tv_price.setTextColor(0xFF333333);
//                                    tv_continued_price.setTextColor(0xFF333333);
//                                }else{
//                                    tv_credit_score_desc.setVisibility(View.VISIBLE);
//                                    tv_credit_score_desc2.setVisibility(View.VISIBLE);
//                                    tv_price.setTextColor(0xFFFD555B);
//                                    tv_continued_price.setTextColor(0xFFFD555B);
//                                }
//
//                                tv_cycling_time.setText(""+bean.getCycling_time());
//
//                                if(bean.getCar_type()==1){
//                                    iv_bike.setImageResource(R.drawable.bike_icon2);
//                                }else{
//                                    iv_bike.setImageResource(R.drawable.ebike_icon2);
//                                }
//
//                                if("0.00".equals(bean.getOrder_amount()) || "0".equals(bean.getOrder_amount())){
//                                    rl_payment_name.setVisibility(View.GONE);
//                                    rl_payment_time.setVisibility(View.GONE);
//                                }else{
//                                    rl_payment_name.setVisibility(View.VISIBLE);
//                                    rl_payment_time.setVisibility(View.VISIBLE);
//                                }
//
//                            }
//
//                        } catch (Exception e) {
////                            memberEvent(context.getClass().getName()+"_"+e.getStackTrace()[0].getLineNumber()+"_"+e.getMessage());
//                        }
//
//                    }
//                });
//            }
//        });

    }


    @Override
    public void onClick(View v) {
        String uid = SharedPreferencesUrls.getInstance().getString("uid","");
        String access_token = SharedPreferencesUrls.getInstance().getString("access_token","");
        switch (v.getId()){
            case R.id.ll_backBtn:
//                if(!isOrder){
//                    Intent intent = new Intent(context, MainActivity.class);
////                  intent.putExtra("flag", true);
//                    startActivity(intent);
//                }

                scrollToFinishActivity();
                break;

            case R.id.bt_unbind:
                if (access_token == null || "".equals(access_token)){
                    Toast.makeText(context,"请先登录账号",Toast.LENGTH_SHORT).show();
                    return;
                }

                Log.e("rid===", "===");

                CustomDialog.Builder customBuilder = new CustomDialog.Builder(context);
                customBuilder.setTitle("温馨提示").setMessage("是否要解绑车辆"+codenum+"?")
                        .setNegativeButton("否", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();

                            }
                        }).setPositiveButton("是", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();

                        untieCar();

                    }
                });
                customBuilder.create().show();


                break;
        }
    }

    private void untieCar(){
        RequestParams params = new RequestParams();
//        params.put("uid",uid);
//        params.put("access_token",access_token);
//        params.put("rid",rid);
//        params.put("paytype",paytype);
//        params.put("codenum", "80001");  //URLEncoder.encode("30005061")

        Log.e("untieCar===", "===");

        HttpHelper.post(context, Urls.untieCar + URLEncoder.encode(codenum), null, new TextHttpResponseHandler() {
            @Override
            public void onStart() {
                Log.e("untieCar===onStart", "===");
                if (loadingDialog != null && !loadingDialog.isShowing()) {
                    loadingDialog.setTitle("正在提交");
                    loadingDialog.show();
                }
            }
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                Log.e("untieCar===onFailure", "===");
                if (loadingDialog != null && loadingDialog.isShowing()){
                    loadingDialog.dismiss();
                }
                UIHelper.ToastError(context, throwable.toString());
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                try {
                    Log.e("untieCar===1", "==="+responseString);

                    ResultConsel result = JSON.parseObject(responseString, ResultConsel.class);
//                    if (result.getFlag().equals("Success")) {
////                        osn = result.getData();
////                        if ("1".equals(paytype)){
////                            show_alipay(osn,uid,access_token);
////                        }else {
////                            show_wxpay(osn,uid,access_token);
////                        }
//                    } else {
//                        Toast.makeText(context,result.getMessage(),Toast.LENGTH_SHORT).show();
//                    }

                    Toast.makeText(context,result.getMessage(),Toast.LENGTH_SHORT).show();

                    scrollToFinishActivity();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (loadingDialog != null && loadingDialog.isShowing()){
                    loadingDialog.dismiss();
                }
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

//            if(!isOrder){
//                Intent intent = new Intent(context, MainActivity.class);
////            intent.putExtra("flag", true);
//                startActivity(intent);
//            }


            scrollToFinishActivity();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
